package com.ruoyi.common.utils;

import org.jgrapht.Graph;
import org.jgrapht.alg.cycle.CycleDetector;
import org.jgrapht.alg.cycle.JohnsonSimpleCycles;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;

import java.util.List;

public class GraphUtils {

    public static void getJohnsonSimpleCycles(){
        CycleDetector<String, DefaultEdge> cycleDetector;
        Graph<String, DefaultEdge> g;

        g = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);

        // Add vertices
        g.addVertex("Dev_GetInfo");
        g.addVertex("Dev_Reset");
        g.addVertex("Dev_ModPower");
        g.addVertex("Mod_GetIdVer");
        g.addVertex("Mod_NextCtrlOn");
        g.addVertex("Dev_LEDCtrl");
        g.addVertex("Mod_DetonCtrl15s");
        g.addVertex("Dev_KA1Ctrl");
        g.addVertex("Dev_KA23Ctrl");
        g.addVertex("Dev_KA4Ctrl");

        // Add edges

        g.addEdge("Dev_GetInfo", "Dev_Reset");
        g.addEdge("Dev_Reset", "Dev_Reset");
        g.addEdge("Dev_Reset", "Dev_ModPower");
        g.addEdge("Mod_GetIdVer", "Dev_Reset");
        g.addEdge("Dev_LEDCtrl", "Dev_Reset");
        g.addEdge("Dev_ModPower", "Mod_GetIdVer");
        g.addEdge("Dev_ModPower", "Dev_KA4Ctrl");
        g.addEdge("Dev_LEDCtrl", "Dev_ModPower");
        g.addEdge("Mod_GetIdVer", "Mod_GetIdVer");
        g.addEdge("Mod_GetIdVer", "Mod_NextCtrlOn");
        g.addEdge("Mod_GetIdVer", "Dev_LEDCtrl");
        g.addEdge("Mod_NextCtrlOn", "Mod_GetIdVer");
        g.addEdge("Dev_LEDCtrl", "Mod_DetonCtrl15s");
        g.addEdge("Dev_KA1Ctrl", "Dev_LEDCtrl");
        g.addEdge("Dev_KA4Ctrl", "Dev_LEDCtrl");
        g.addEdge("Mod_DetonCtrl15s", "Dev_KA1Ctrl");
        g.addEdge("Dev_KA1Ctrl", "Dev_KA23Ctrl");
        g.addEdge("Dev_KA23Ctrl", "Dev_KA1Ctrl");
        g.addEdge("Dev_KA23Ctrl", "Dev_KA4Ctrl");
        g.addEdge("Dev_KA4Ctrl", "Dev_KA23Ctrl");

        // Printing the vetrices and the edges
        System.out.println(g.toString());

        JohnsonSimpleCycles jsc = new JohnsonSimpleCycles(g);
        List<List<String>> cycles = jsc.findSimpleCycles();

        if(cycles != null && cycles.size() > 0){
            for(List<String> c : cycles){
                System.out.print("cycle: ");
                for(String e : c)
                    System.out.print(e + ", ");
                System.out.println();
            }

        }
    }
}
